/**
 * Created by win8 on 2016-10-11.
 */
var myScroll, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset, generatedCount = 0, scrollDiv = '';

function loaded() {
    if (scrollDiv == '') {
        scrollDiv = 'wrapper-wfc';
    }

    pullDownEl = $("#" + scrollDiv + " .pullDown");
    pullDownOffset = pullDownEl.height();
    pullUpEl = $("#" + scrollDiv + " .pullUp");
    pullUpOffset = pullUpEl.height();
    if (myScroll)
        myScroll.destroy();
    myScroll = new iScroll(scrollDiv, {
        useTransition: true,
        topOffset: pullDownOffset,
        checkDOMChanges: true,
        onRefresh: function () {
            if (pullDownEl.className == "loading") {
                pullDownEl.className = "";
                pullDownEl.find(".pullDownLabel").text("下拉刷新...");
            } else if (pullUpEl.className == "loading") {
                pullUpEl.className = "";
                pullUpEl.find(".pullUpLabel").text("上拉显示更多...");
            }
        },
        onScrollMove: function () {
            if (this.y > 5 && pullDownEl.className != "flip") {
                pullDownEl.className = "flip";
                pullDownEl.find(".pullDownLabel").text("准备刷新...");
                this.minScrollY = 0;
            } else if (this.y < 5 && pullDownEl.className == "flip") {
                pullDownEl.className = "";
                pullDownEl.find(".pullDownLabel").text("下拉刷新...");
                this.minScrollY = -pullDownOffset;
            } else if (this.y < (this.maxScrollY - 5) && pullUpEl.className != "flip") {
                pullUpEl.className = "flip";
                pullUpEl.find(".pullUpLabel").text("准备刷新...");
                this.maxScrollY = this.maxScrollY;
            } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className == "flip") {
                pullUpEl.className = "";
                pullUpEl.find(".pullUpLabel").text("上拉显示更多...");
                this.maxScrollY = pullUpOffset;
            }
        },
        onScrollEnd: function () {
            if (pullDownEl.className == "flip") {
                pullDownEl.className = "loading";
                pullDownEl.find(".pullDownLabel").text("Loading...");
                //pullDownAction(param);
            } else if (pullUpEl.className == "flip") {
                pullUpEl.className = "loading";
                pullUpEl.find(".pullUpLabel").text("上拉显示更多...");
                pullUpEl.find('.pullUpLabel').text('Loading...');
                pullUpAction(); //当到底部时再上拉时加载数据// Execute custom function (ajax call?)
            }
        }
    });
    //loadAction();
}

// iscrool事件初始化
function isScrollDevice() {
    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
    }, false);//阻止冒泡
    document.addEventListener('DOMContentLoaded', function () {
        setTimeout(loaded, 0);
    }, false);
}

window.onload = isScrollDevice();

//初始状态，加载数据
function loadAction() {
    var el, item;
    el = $("#" + scrollDiv + " .recordlist");
    for (var i = 0; i < 10; i++) {
        item = document.createElement('div');
        item.innerHTML = " <div class='item-header' style='border-bottom:none'>2016.8.20</div>" +
            "<div class='sgls-item'>" +
            "<div>" +
            "<p>" +
            "<span class='title'>申购单号：</span><span>D0000001</span>" +
            "</p>" +
            "<p>" +
            "<span class='title pull-left'>申购部门：</span><span>前厅部门</span>" +
            "<span class='pull-right'><i class='title'>制单人：</i>" +
            "<i class='text-nowrap'>李小姐</i></span></p>" +
            "<p>" +
            "<span class='title'>申购时间：</span><span>2016.7.22 16:00</span>" +
            "</p>" +
            "</div>" +
            "<div class='check text-center'>" +
            "<i class='icon-ok'></i>" +
            "</div>" +
            "</div>";
        //debugger;
        el.append(item);
    }
    myScroll.refresh();
}

//下拉刷新当前数据
function pullDownAction() {
    setTimeout(function () {
        //这里执行刷新操作
        myScroll.refresh();
    }, 400);
}

//上拉加载更多数据
function pullUpAction() {
    setTimeout(function () {
        var el, item;
        el = $("#" + scrollDiv + " .recordlist");
        for (var i = 0; i < 10; i++) {
            if (scrollDiv == 'wrapper-wfc') {
                item = "<a href='peisongdanxq-wfp.html' class='sgls-item mt1'>";
            } else
                item = "<a href='peisongdanxq.html' class='sgls-item mt1'>";

            item += "<div>" +
                "<p>" +
                "<span class='title'>单据编号1：</span><span>配送-D0000001</span>" +
                "</p>" +
                "<p>" +
                "<span class='title pull-left'>要货门店：</span><span class='span3'>三香店</span>" +
                "<span class='pull-right'><i class='title'>金额：</i><i class='text-nowrap'>63800.00</i></span>" +
                "</p>" +
                "<p>" +
                "<span class='title'>下单时间：</span><span>2016.7.22 16:00</span>" +
                "</p>" +
                "<p>" +
                "<span class='title'>要求到货时间：</span><span>2016-07-07</span>" +
                "<span class='pull-right'><i class='title'>下单人：</i><i>李小姐</i></span>" +
                "</p>" +
                "</div>";

            if (scrollDiv == 'wrapper-wfc') {
                item += "<div class='check text-center'>" +
                    "<i class='icon-ok'></i>" +
                    "</div>";
            }
            item += "</a>";
            el.append(item);
        }
        myScroll.refresh();
    }, 400);
}

function changePull() {
    setTimeout(loaded, 100);
}
